package com.itheima.Utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.itheima.common.CustomException;


/**
 * 短信发送工具类
 */
public class SMSUtils {

	/**
	 * 发送短信
	 * @param phoneNumbers 手机号
	 * @param param 参数
	 */
	public static void sendMessage(String phoneNumbers,String param){  //电话号码  验证码
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G5rJZuQdW127QJHRexk", "5EeLLxPuImJJXCHvNPBMrdKkydw19M");
		IAcsClient client = new DefaultAcsClient(profile);

		SendSmsRequest request = new SendSmsRequest();
		request.setSysRegionId("cn-hangzhou");
		request.setPhoneNumbers(phoneNumbers);     // 电话号码
		request.setSignName("传智健康");           // 签名
		request.setTemplateCode("SMS_198930661");  // 模板
		request.setTemplateParam("{\"code\":\""+param+"\"}");   //验证码 替换 模板中 ${code} 占位符
		try {
			SendSmsResponse response = client.getAcsResponse(request);
			System.out.println("短信发送成功");
		} catch (ClientException e) {
			e.printStackTrace();
			throw new CustomException("网络异常，请重试");
		}
	}

	public static void main(String[] args) {
		// SMSUtils.sendMessage("手机号", "9999");
	}

}
